﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ServerApp.DAO;
using YServer;

namespace ServerApp.Cache
{
    public class RoleCache
    {
        AccountCache a_cache = CacheFactory.accountCache;

        /// <summary>
        /// 账号id与角色的绑定字典
        /// </summary>
        public Dictionary<int, Role> aid_roleDic = new Dictionary<int, Role>();

        //角色缓存字典
        public Dictionary<string, Role> name_roleDic = new Dictionary<string, Role>();

        /// <summary>
        /// 角色名字与连接的绑定字典（单独聊天、加好友）
        /// </summary>
        public Dictionary<string, YToken> roleOnlineDic = new Dictionary<string, YToken>();

        /// <summary>
        /// 根据名字使角色缓存字典初始化(新增数据)
        /// </summary>
        /// <param name="name"></param>
        void Init(string name) { 
            //若缓存中有，返回
            if (name_roleDic.ContainsKey(name)) return;

            //否则读取数据库，并加入aid_roleDic与name_roleDic中
        }
        /// <summary>
        /// 根据名字使角色缓存字典初始化(新增数据)
        /// </summary>
        /// <param name="name"></param>
        void Init(int id)
        {
            //若缓存中有，返回
            if (aid_roleDic.ContainsKey(id)) return;

            //否则读取数据库，并加入aid_roleDic与name_roleDic中
        }


        /// <summary>
        /// 判断账号是否有角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasRole(int id) {
            //先初始化（注：初始化函数init()有两个重载，根据需要调用）
            Init(id);
            
            //初始化后有
            if (aid_roleDic.ContainsKey(id)) { return true; }

            //没有的话返回false
            return false;
        }

        /// <summary>
        /// 判断角色名是否存在
        /// </summary>
        /// <returns></returns>
        public bool HasRole(string name) {
            //先初始化（注：初始化函数init()有两个重载，根据需要调用）
            Init(name);

            //初始化后有
            if (name_roleDic.ContainsKey(name)) { return true; }

            return false;
        }

        /// <summary>
        /// 判断角色是否在线
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool IsOnline(int id) {

            if (!HasRole(id)) { return false; }

            return roleOnlineDic.ContainsKey(aid_roleDic[id].name);
        }

        /// <summary>
        /// 根据账号id进行角色登录
        /// </summary>
        /// <param name="id"></param>
        /// <param name="token"></param>
        public void RoleLogin(int id,YToken token) {
            //没有角色，返回（严谨）
            if (!HasRole(id)) { return; }

            if (IsOnline(id)) { return; }

            roleOnlineDic.Add(aid_roleDic[id].name, token);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="token"></param>
        /// <param name="id"></param>
        /// <param name="name"></param>
        public void RoleCreate(YToken token,int id, string name) { 
            //保持严谨性,作出判断(上层逻辑写的好，实际上可以省略)
            //如果连接有角色
            if (HasRole(id)) { return; }

            //如果角色名存在
            if (HasRole(name)) { return; }

            //创建角色
            Role roleModel = new Role(id, name);
            //写入缓存
            aid_roleDic.Add(id, roleModel);
            name_roleDic.Add(name, roleModel);
            //写入数据库
        }

        /// <summary>
        /// 离线（传入角色名）
        /// </summary>
        /// <param name="name"></param>
        public void offline(string name) {
            //roleOnlineDic
            if (roleOnlineDic.ContainsKey(name)) {
                roleOnlineDic.Remove(name);
            }
        }

        /// <summary>
        /// 离线（传入账号id）
        /// </summary>
        /// <param name="aid"></param>
        public void offline(int aid)
        {
            if (aid_roleDic.ContainsKey(aid))
            {
                offline(aid_roleDic[aid].name);
            }
        }

        /// <summary>
        /// 根据连接获取角色名字
        /// </summary>
        /// <param name="token"></param>
        public string GetRoleNameByToken(YToken token) { 
            //判断连接是否有账号
            int aid= a_cache.GetAccIdByToken(token);
            if (aid == -10) { return null; }
            //判断角色是否在线
            if(!IsOnline(aid)){ return null; }

            return aid_roleDic[aid].name;
        }


    }
}
